package com.aaa.controller.fpa;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import com.aaa.controller.BaseController;
import com.aaa.entity.Result;
import com.aaa.entity.fpa.ResultDiv;
import com.aaa.entity.fpa.TestPlanFpa;
import com.aaa.entity.fpa.ViewTestResultFpa;
import com.aaa.entity.system.PageOrder;
import com.aaa.service.fpa.ViewResultService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * @author ：姚建军（2511037015@qq.com）
 * @date ：：Created in 2022/9/5 9:44
 * @description：
 * @modified By：
 * @version:
 */
@RestController
@RequestMapping("view")
public class ViewResultFpaController extends BaseController {
    @Autowired
    ViewResultService viewResultService;

    @RequestMapping("toShowTestResultFpa.do")
    public ModelAndView toShowPlanFpa1(ModelAndView modelAndView){
        modelAndView.setViewName("fpa/showTestResultFpa");
        return modelAndView;
    }
    @RequestMapping("toShowEcharts.do")
    public ModelAndView toShowPlanFpa2(ModelAndView modelAndView){
        modelAndView.setViewName("fpa/showEcharts");
        return modelAndView;
    }

    @RequestMapping("query.do")
    public String queryByPage(PageOrder pageOrder, ViewTestResultFpa viewResultFpa) {
        Page page = viewResultService.queryAll(pageOrder, viewResultFpa);
        JSONConfig jsonConfig = JSONConfig.create();
        jsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        String jsonStr = JSONUtil.toJsonStr(success(page.getTotal(), page.getResult()), jsonConfig);
        return jsonStr;
    }


    @RequestMapping("queryTestPlanId.do")
    public List queryTestPlanId() {
        List<ViewTestResultFpa> viewTestResultFpa = viewResultService.queryTestPlanId();
        return  viewTestResultFpa;
    }

    @RequestMapping("delete.do")
    public Result delete(int[] testerId) {
        boolean b = viewResultService.deleteById(testerId);
        boolean Id = viewResultService.deleteByTesterId(testerId);
        if (b&&Id) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    /**
     * 查看页面显示
     * @param id
     * @return
     */
    @RequestMapping("div.do")
    public Result resultDiv(Integer id){
        List<ResultDiv> resultDivs = viewResultService.queryAllResultDiv(id);
        return success(resultDivs);
    }

    /**
     *报表统计
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "queryResultToReport.do")
    public Result queryResultToReport(){
        Map map = viewResultService.queryAllPlanFpa();
        return success(map);
    }
    /**
     * 结果显示页面
     * @param id
     * @return
     */
    @RequestMapping("queryResult.do")
    public Result queryResult(int id,String type) {
        List<ViewTestResultFpa> list = viewResultService.queryResult(id,type);
        if(ObjectUtil.isNotEmpty(list)){
            for (ViewTestResultFpa viewTestResultFpa : list) {
                return  success(viewTestResultFpa);
            }
        }
        return fail();
    }



    @RequestMapping("queryQuestionFPA.do")
    public Result getQuestionMHT(PageOrder pageOrder , Integer id) {
        Page<Map<String, Object>> byTesterId = viewResultService.getByTesterId(pageOrder, id);
        if (ObjectUtil.isNotEmpty(byTesterId)) {
            return success(byTesterId.getTotal(), byTesterId.getResult());
        }
        return fail();
    }

    /**
     *查询id
     * @return
     */
    @RequestMapping("pullFpa.do")
    public Result pullFpa(){
        List<TestPlanFpa> viewTestResultFpa = viewResultService.queryAllResultFpa();
        return success(viewTestResultFpa);
    }
}
